<?php
/**
 * Description of MDimType class
 *
 * @author Chisheng Lu
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-03-22
 * @version 1.0
 */

class MDimType {
    private $mysqli;

    function  __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");

    }

    /**
     *
     * @param Integer $intMDimTypeId
     * @return Array
     */
    public function getSingleMDimType($intMDimTypeId){
        //檢查向度編號是否為空白
        if (empty ($intMDimTypeId)) {
            throw new Exception('缺少類別編號！');
        }

        $sql_selMDimType = 'SELECT * FROM `MDimType` WHERE `MDTId`=' . $intMDimTypeId;
        $rs_selMDimType = $this->mysqli->query($sql_selMDimType);

        if(!$rs_selMDimType){
            throw new Exception('無法查詢單一類別資料！');
        }else{
            if($rs_selMDimType->num_rows == 0){
                throw new Exception('無指定編號的類別資料！');
            }
            $row_selMDimType = $rs_selMDimType->fetch_row();
            return $row_selMDimType;
        }
    }

    /**
     *
     * @param Integer $intMDId
     * @return Array
     */
    public function getMDimTypeListByMDId($intMDId) {

        $aryMDimTypes = array();

        $sql_getMDimTypeList = 'SELECT * FROM `MDimType` WHERE `MDId`=' . $intMDId;
        $rs_getMDimTypeList = $this->mysqli->query($sql_getMDimTypeList);

        if(!$rs_getMDimTypeList){
            throw new Exception('無法查詢類別資料！');
        }else{

            while($row_getMDimTypeList = $rs_getMDimTypeList->fetch_row()){
                array_push($aryMDimTypes, $row_getMDimTypeList);
            }

            //釋放查詢結果
            $rs_getMDimTypeList->free_result();
        }

        return $aryMDimTypes;
    }

    /**
     *
     * @param Integer $intMDId
     * @param String $strEngName
     * @param String $strChtName
     * @param Integer $intUserId
     * @return Boolean
     */
    public function addMDimType($intMDId, $strEngName, $strChtName, $intUserId) {
        $bolRtn = false;
        //先查詢是否已有相同名稱
        if(empty ($strEngName) || empty($strChtName)){
            throw new Exception('類別名稱不可為空白！');
        }

        $sql_checkDuplicateValue = 'SELECT * FROM `MDimType` WHERE `MDTEngName`=\''.$strEngName.'\' OR `MDTChtName`=\''.$strChtName.'\''. ' AND `MDId` = ' . $intMDId;
        $rs_checkDuplicateValue = $this->mysqli->query($sql_checkDuplicateValue);

        //檢查是否已有重複值
        if(!$rs_checkDuplicateValue){
            throw new Exception('查詢是否有重複時發生錯誤！');
        }else{
            if($rs_checkDuplicateValue->num_rows != 0){
                throw new Exception('已有相同的類別名稱存在！');
            }else{
                $strNow = $this->getNowTime();
                $sql_addMDimType = 'INSERT INTO `MDimType` SET `MDTEngName`=\''.$strEngName.'\', `MDTChtName`=\''.$strChtName.'\', `UserId`='. $intUserId . ', `MDId`='.$intMDId. ', `MDTCreated`=\''.$strNow.'\'';
                $rs_addMDimType = $this->mysqli->query($sql_addMDimType);
                if(!$rs_addMDimType){
                    throw new Exception('新增類別資料時發生錯誤！');
                }else{
                    $bolRtn = true;
                }
            }
        }

        return $bolRtn;
    }

    /**
     *
     * @param Integer $intMDimTypeId
     * @param Integer $intMDId
     * @param String $strEngName
     * @param String $strChtName
     * @param Integer $intUserId
     * @return Boolean
     */
    public function editMDimType($intMDimTypeId, $intMDId, $strEngName, $strChtName, $intUserId) {
        $bolRtn = false;
        //檢查是否有類別編號
        if(empty ($intMDimTypeId)){
            throw new Exception('缺少類別編號！');
        }

        //檢查類別名稱是否為空白
        if(empty ($strEngName) || empty ($strChtName)){
            throw new Exception('類別名稱不可為空白！');
        }

        //更新類別資料
        $strNow = $this->getNowTime();
        $sql_uptMDimType = 'UPDATE `MDimType` SET `MDId`='.$intMDId. ', `MDTEngName`=\''.$strEngName.'\', `MDTChtName`=\''.$strChtName.'\', `UserId`='.$intUserId . ', `MDTModified`=\''.$strNow.'\' WHERE `MDTId`='.$intMDimTypeId;
        if(!$this->mysqli->query($sql_uptMDimType)){
            throw new Exception('更新類別資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }

        return $bolRtn;
    }

    /**
     *
     * @param Integer $intMDimTypeId
     * @return Boolean
     */
    public function delMDimType($intMDimTypeId) {
        $bolRtn = false;
        $intMDId = 0;
        //檢查類別編號是否為空白
        if (empty ($intMDimTypeId)) {
            throw new Exception('缺少類別編號！');
        }
        //檢查類別是否為使用中，使用中不允許刪除
        $sql_chkUsed0 = 'SELECT * FROM `MDimType` WHERE `MDTId`=' . $intMDimTypeId;
        $rs_chkUsed0 = $this->mysqli->query($sql_chkUsed0);
        if(!$rs_chkUsed0){
                throw new Exception('異常！查詢類別是否為使用中時發生錯誤。');
        } else {
             if($rs_chkUsed0->num_rows == 0){
                 //throw new Exception('類別項目正在使用中，不允許刪除。');
                 $bolRtn = true;
             }else{
                 $row_getUsed0 = $rs_chkUsed0->fetch_row();
                 $intMDId = $row_getUsed0[0];
                 $sql_chkUsed = 'SELECT * FROM `SQMCDIM` WHERE `DimCode`=' . $intMDId;
                 $rs_chkUsed = $this->mysqli->query($sql_chkUsed);
                 if(!$rs_chkUsed){
                     throw new Exception('異常！查詢類別是否為使用中時發生錯誤。');
                }else{
                    if($rs_chkUsed->num_rows > 0){
                        throw new Exception('類別項目正在使用中，不允許刪除。');
                    }else{
                        //非使用中的向度，先刪除該向度下類別在刪除向度本身
                        $sql_delType = 'DELETE FROM `MDimType` WHERE `MDTId`=' . $intMDimTypeId;
                        if(!$this->mysqli->query($sql_delType)){
                            throw new Exception('錯誤！刪除向度下的類別時發生錯誤。');
                        }else{
                            //刪除向度下類別後再刪除該向度
                            //$sql_delMDimType ='DELETE FROM `MDimType` WHERE `MDId`=' . $intMDimTypeId;
                            //if(!$this->mysqli->query($sql_delSDimLevel)){
                            //    throw new Exception('錯誤！已刪除向度下類別，於刪除類別時發生錯誤。');
                            //}else{
                            //    $bolRtn = true;
                            //}
                        }
                        $bolRtn = true;
                    }
                }
             }
        }

        return $bolRtn;
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    /**
     *
     * @param Integer $intMDimTypeId
     * @return Integer
     */
    public function getTypeTotalNumById($intMDimTypeId){
        $intTypeCountNum = 0;
        $intMDId = 0;
        if (empty ($intMDimensionId)) {
            throw new Exception('缺少向度編號！');
        }

        $sql_getMDimId = 'SELECT MDId FROM `MDimType` WHERE `MDTId`=' . $intMDimTypeId;
        $rs_getMDimId = $this->mysqli->query($sql_getMDimId);
        if(!$rs_getMDimId){
            throw new Exception('查詢向度下類別數時發生錯誤！');
        }else{
            if($rs_getMDimId->num_rows > 0){
                $sql_getTypeNum = 'SELECT Count(*) FROM `MDimType` WHERE `MDId`=' . $intMDId;
                $rs_getTypeNum = $this->mysqli->query($sql_getTypeNum);
                if(!$rs_getTypeNum){
                    throw new Exception('查詢向度下類別數時發生錯誤！');
                }else{
                    $row_getTypeNum = $rs_getTypeNum->fetch_row();
                    $intTypeCountNum = $row_getTypeNum[0];
                }
            }
        }
        return $intTypeCountNum;
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
